/**
 * [分页]
 * @param {[Object]} options [设置]
 * options： {
 *     len：每页的记录条数，
 *     $el: 分页的外层容器，
 *     callback: 回调函数，
 *     params: 回调函数中所需要的参数，
 *             params在经过此插件后会添加start,len这两个属性,
 *             start是记录开始下标（回调函数中可以自己处理成页码）， 每页的记录条数
 *     total: 总记录数， 可选
 * }
 */
function UpLoadPage(options) {

    var $container = '<ul class="paging"></ul>',
        $prev = '<li class="page-prev paging_but paging_off"><a href="javaScript:">上一页</a></li>',
        $next = '<li class="page-next paging_but"><a href="javaScript:">下一页</a></li>',
        $trans = '<li>(<span class="cur-page">0</span>/<span class="total-page">0</span>)</li>' +
            '<li><input class="w30 h30 box-sizing tc paging_input" type="text"></li>' +
            '<li class="paging_but page-trans"><a href="javaScript:">跳转</a></li>';

    var cur = options.curpage, max = 0,
        len = options.len || 10,
        total = options.total || 0,
        params = options.params || {},
        callback = options.callback,
        $el = options.$el;

//    var currentPage = $el.find(".currentPage").text();

//    console.log(currentPage);
//    var pagePrev = html + currentPage+".html";

    function getEventTag(e) {
        e = e || window.event;
        return e.target || e.srcElement;
    }

    function pageShow($pgs, i) {
        var start = i * len;
        cur = i;
        params.start = start;
        params.len = len;
        params.first = false;
        params.flag = 0;
        callback(params);
        console.log(i);
        console.log(start);
        $pgs.hide();

        var html = "";
        var rtype = $("#selResource>a.on").data("type")
        var schID = ($("#selectSch>a.on").attr("selectid")==0) ? "" : $("#selectSch>a.on").attr("selectid");
        var subID = ($("#selectSub>a.on").attr("selectid")==0) ? "" : $("#selectSub>a.on").attr("selectid");
        var areaID = ($("#selectArea>a.on").attr("selectid")==0)? "": $("#selectArea>a.on").attr("selectid");
        var order  = ($("#selectOrder>a>span.fb").attr("data-type")==0) ? 1: $("#selectOrder>a>span.fb").attr("data-type");
        html = rtype + "_" + schID + "_" + subID + "_" + areaID + "_"+order;
        html = options.url+ html;
        console.log(html);
        window.location.href = html+"_"+(parseInt(i) + 1)+".html";

    }

    function checkVal(val) {
        var reg = /^[1-9]\d*$/;
        return reg.test(val) && val <= max;
    }

    this.setPage = function(total) {

        var html = "";
        var rtype = $("#selResource>a.on").data("type")
        var schID = ($("#selectSch>a.on").attr("selectid")==0) ? "" : $("#selectSch>a.on").attr("selectid");
        var subID = ($("#selectSub>a.on").attr("selectid")==0) ? "" : $("#selectSub>a.on").attr("selectid");
        var areaID = ($("#selectArea>a.on").attr("selectid")==0)? "": $("#selectArea>a.on").attr("selectid");
        var order  = ($("#selectOrder>a>span.fb").attr("data-type")==0) ? 1: $("#selectOrder>a>span.fb").attr("data-type");
        html = rtype + "_" + schID + "_" + subID + "_" + areaID + "_"+order;
        html = options.url+ html;
        console.log(html);


        var time = (new Date()).getTime();
        var uls = "", istart, iend;
        max = Math.ceil(total / len);
        if (max > 1) {
            cur = cur < 0 ? 0 : cur > max - 1 ? max - 1 : cur;
            if (cur >= 5 && cur < max - 6) {
                istart = cur - 4;
                iend = cur - 4 +9;
            } else if (cur < 5) {
                istart = 0;
                iend = 9;
            } else if (cur >= max - 6) {
                istart = max - 10;
                iend = max - 1;
            }
            istart = istart < 0 ? 0 : istart;
            iend = iend >= max ? max - 1 : iend;
            $el.html("").append($container);
            var $ec = $el.find(".paging");
            $ec.append($prev);
            for (var i = istart; i <= iend; i++) {
                var  href = null;
                href = html+"_"+(parseInt(i) + 1)+".html";
                $ec.append('<li class="page" data-i="' + i + '"><a class="' + (i == cur ? 'currentPage' : '') + '" href="'+href+'">' + (i + 1) + '</a></li>');
            }
            $ec.append($next);
            $ec.append($trans);
            $el.find(".page-prev").removeClass("paging_off");
            var pagePrevHref = html+"_"+(parseInt(cur))+".html";
            $el.find(".page-prev>a").attr("href",pagePrevHref);
            $el.find(".page-next").removeClass("paging_off");
            var pageNextHref = html+"_"+(parseInt(cur)+2)+".html";
            $el.find(".page-next>a").attr("href",pageNextHref);
            if (cur == 0) {
                $el.find(".page-prev").addClass("paging_off");
            } else if (cur >= max - 1) {
                $el.find(".page-next").addClass("paging_off");
            }
            var currentPage = $(".currentPage").text();

            $(".cur-page").text(parseInt(cur) + 1);
            $(".total-page").text(max);
        }
    }

    this.init = function() {
        params.start = 0;
        params.len = len;
        params.first = true;
        params.page = this;
        $el.html("");
        callback(params);
        if (total) {
            this.setPage(total);
        }

        $el.off();
        $el.click(function(e) {
            var srcE = getEventTag(e);
            var $p = $(srcE).parent();
            var i = 0;
            if ($p.hasClass("page") && !$(srcE).hasClass("currentPage")) {
                i = $p.attr("data-i");
            } else if($p.hasClass("paging_off")) {
                return false;
            } else if ($p.hasClass("page-prev")) {
                i = $el.find("a.currentPage").parent(".page").prev(".page").attr("data-i");
            } else if ($p.hasClass("page-next")) {
                i = $el.find("a.currentPage").parent(".page").next(".page").attr("data-i");
            } else if ($p.hasClass("page-trans")) {
                var val = $el.find(".paging_input").val();
                if (checkVal(val)) {
                    i = val - 1;
                } else {
                    alert("请输入正确的页码！");
                }
            } else {
                return false;
            }
            var $ec = $el.find(".paging");
            pageShow($ec, i);
        })
    }
}